var path = require("path"),
    webpack = require("webpack"),
    HtmlWebpackPlugin = require("html-webpack-plugin"),
    ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
        context: path.resolve(__dirname, './static'),
        devtool: "source-map",
        entry: {
            app: "./js/index"
        },
        output: {
            path: path.resolve(__dirname, "./dist"),
            publicPath: "./",
            filename: "js/[name].js"
        },
        resolve: {
            extensions: ['.js', '.css', '.png', 'jpg', '.gif'], //后缀名自动补全
        },
        module: {
            rules: [
                {
                    test: /\.js$/,
                    use: {
                        loader: "babel-loader"
                    },
                    exclude : "/node_modules/"
                },{
                    test:/\.css$/,
                    use:ExtractTextPlugin.extract({
                        fallback: "style-loader",
                        use: [{
                            loader: "css-loader",
                            options: {
                                name : "css/[name].[exe]",
                                publicPath : "./css/"
                            }
                        }]
                    })
                },
                { test: /\.jpg$/,
                    use:{
                         loader: "file-loader",
                         options:{
                            name:"img/[name].[ext]",
                            publicPath:"../"
                        } 
                    }
                }
            ]
        },
        plugins :[
            new webpack.BannerPlugin('版权所有，翻版必究'),
            new webpack.optimize.CommonsChunkPlugin({
                name : "common",
                fliename:"common.js",
                minChunks :2
            }),// 默认会把所有入口节点的公共代码提取出来,生成一个common.js
            new ExtractTextPlugin("css/[name].css"),
            new webpack.NoEmitOnErrorsPlugin(),//解决阻塞,编译完成再报错
            new webpack.optimize.OccurrenceOrderPlugin(),//【排序输出】
            new webpack.optimize.UglifyJsPlugin({//【压缩】
                compress: {
                    warnings: false
                }
            }),
            new HtmlWebpackPlugin({
                title:'webpack test',
                template: 'html-withimg-loader!'+path.resolve("./static/",'index.html'), // 源模板文件
                filename: 'index.html', // 输出文件【注意：这里的根路径是module.exports.output.path】
                showErrors: true,
                inject: 'body',
                chunks: "all",
                minify: false,
                hash: true
            }),
        ]
    };